import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


/**
 * java -cp bin:lib/mysql-connector-java-5.1.16-bin.jar MysqlTest
 * 
 * Download from:
 * http://dev.mysql.com/downloads/connector/j/
 */
public class MysqlTest
{
    static public final String HOST = "localhost";
    static public final int    PORT = 3306;
    static public final String DB_NAME = "mysql";
    static public final String USER = "root";
    static public final String PASS = "";
    
    public static void main(String args[]){
        Statement stmt;

        //Register the JDBC driver for MySQL.
        try {
            Class.forName("com.mysql.jdbc.Driver");

            //Define URL of database server for database named mysql on the localhost
            // with the default port number 3306.
            String url = "jdbc:mysql://" + HOST + ":" + PORT + "/" + DB_NAME;

            //Get a connection to the database for a user named root with a blank password.
            // This user is the default administrator having full privileges to do anything.
            Connection con = DriverManager.getConnection(url, USER, PASS);

            //Display URL and connection information
            System.out.println("URL: " + url);
            System.out.println("Connection: " + con);

            //Get a Statement object
            stmt = con.createStatement();
            
            System.out.println("* List of databases:");
            ResultSet results = stmt.executeQuery("SHOW DATABASES;");
            while (results.next()) {
                System.out.print( "\t" + results.getString(1) );
                System.out.println("");
            }
            
            System.out.print("Switching to other db : ");
            int result = stmt.executeUpdate("USE potholes");
            System.out.print(result + "\n");
            
            System.out.print("Deleting a record : ");
            result = stmt.executeUpdate("DELETE FROM people WHERE id > 0;");
            System.out.print(result + "\n");
            
            System.out.print("Adding a record : ");
            result = stmt.executeUpdate("INSERT INTO people (name,size,age) VALUES ('David',3.15,28);");
            System.out.print(result + "\n");
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
